<template>
  <div v-if="!isMain && !isView" class="add-equip-detail add-inspect-plan">
    <div style="padding: 16px 0">
      <px-button type="primary" icon="ArrowLeft" @click="backMainPage">返回</px-button>
    </div>
    <div class="add-inspect-plan-wrapper">
      <px-scrollbar ref="scrollbarRef" class="add-equip-content" style="padding: 0" @scroll="handleScroll">
        <!-- ref="basicsRef" -->
        <px-form ref="formRef" :inline="true" :model="formValue" :rules="rules" label-position="right" label-width="120px">
          <div ref="basicsRef">
            <EditDropDown dropdownTitle="基本信息" style="margin-top: 8px">
              <template v-slot:content>
                <px-row>
                  <px-col :span="12">
                    <px-form-item label="改善编号" prop=" improveId">
                      <px-input v-model="formValue.improveId" placeholder="自动生成改善编号" maxlength="50" disabled />
                    </px-form-item>
                  </px-col>
                  <px-col :span="12">
                    <px-form-item label="改善内容" prop="improveName">
                      <px-input v-model="formValue.improveName" placeholder="请输入改善内容" />
                    </px-form-item>
                  </px-col>
                  <!-- 科室 -->
                  <px-col :span="12">
                    <EquipClassSelect :isRequired="true" :className="formValue.className" @changeClassName="changeClassName" />
                  </px-col>
                  <!-- 系统 -->
                  <px-col :span="12">
                    <EquipSystemTree
                      propValue="systemId"
                      :isRequired="true"
                      :className="formValue.className"
                      :systemCode="formValue.systemId"
                      @changeAllSystemCodes="changeAllSystemCodes"
                      @changeSystemCodes="changeSystemCodes"
                      @clearSystem="clearSystem"
                    />
                  </px-col>
                  <px-col :span="12">
                    <px-form-item label="改善来源" prop="improveResource">
                      <px-radio-group v-model="formValue.improveResource">
                        <px-radio v-for="(item, index) in props.rectifyStatusList" :key="index" :label="item.value" :value="item.value">{{
                          item.name
                        }}</px-radio>
                      </px-radio-group>
                    </px-form-item>
                  </px-col>
                  <px-col :span="12">
                    <px-form-item label="关联单号" prop="relationNumber">
                      <px-input
                        v-model="formValue.relationNumber"
                        placeholder="请输入关联单号"
                        maxlength="50"
                        :disabled="formValue.improveResource === 'CUSTOM'"
                        clearable
                      />
                    </px-form-item>
                  </px-col>
                  <px-col :span="12">
                    <px-form-item label="原因类型" prop="reasonType">
                      <px-select v-model="formValue.reasonType" placeholder="请选择原因类型" filterable clearable>
                        <px-option v-for="(item, index) in missingLevelList" :key="index" :label="item.name" :value="item.value" />
                      </px-select>
                    </px-form-item>
                  </px-col>
                  <px-col :span="12">
                    <px-form-item label="改善必要性" prop="necessaryLevel">
                      <px-radio-group v-model="formValue.necessaryLevel">
                        <px-radio v-for="(item, index) in props.missingTypeList" :key="index" :label="item.value">{{ item.name }}</px-radio>
                      </px-radio-group>
                    </px-form-item>
                  </px-col>
                  <px-col :span="12">
                    <px-form-item label="适用范围" prop="applicableScope">
                      <px-input v-model="formValue.applicableScope" placeholder="请输入适用范围" maxlength="50" clearable />
                    </px-form-item>
                  </px-col>
                  <px-col :span="12">
                    <px-form-item label="新建厂follow" prop="newFactoryFlag">
                      <px-radio-group v-model="formValue.newFactoryFlag">
                        <px-radio v-for="(item, index) in props.reportUserList" :key="index" :label="item.name">{{ item.label }}</px-radio>
                      </px-radio-group>
                    </px-form-item>
                  </px-col>
                  <px-col :span="12">
                    <px-form-item label="改善状态" prop="improveStatus">
                      <px-select v-model="formValue.improveStatus" placeholder="请选择改善状态" filterable clearable>
                        <px-option v-for="(item, index) in missingResourceList" :key="index" :label="item.name" :value="item.value" />
                      </px-select>
                    </px-form-item>
                  </px-col>
                  <px-col :span="12">
                    <px-form-item label="改善人">
                      <px-select-v2 v-model="formValue.improveUser" filterable clearable :options="missingOwnerList" placeholder="请选择改善人" />
                    </px-form-item>
                  </px-col>
                </px-row>
              </template>
            </EditDropDown>
          </div>
          <div ref="improveViewRef">
            <EditDropDown dropdownTitle="问题详情">
              <template v-slot:content>
                <px-row>
                  <px-col :span="24">
                    <px-form-item label="问题描述" prop="problemDesc">
                      <px-input
                        v-model="formValue.problemDesc"
                        placeholder="请输入问题描述"
                        type="textarea"
                        show-word-limit
                        maxlength="1000"
                        clearable
                      />
                    </px-form-item>
                  </px-col>
                  <px-col :span="24">
                    <px-form-item label="根本原因" prop="rootCause">
                      <px-input
                        v-model="formValue.rootCause"
                        placeholder="请输入根本原因"
                        maxlength="1000"
                        show-word-limit
                        type="textarea"
                        clearable
                      />
                    </px-form-item>
                  </px-col>
                  <px-col :span="24">
                    <px-form-item label="影响范围" prop="influenceScope">
                      <px-input
                        v-model="formValue.influenceScope"
                        placeholder="请输入影响范围"
                        maxlength="1000"
                        show-word-limit
                        type="textarea"
                        clearable
                      />
                    </px-form-item>
                  </px-col>
                  <px-col :span="24">
                    <px-form-item label="附件上传" prop="remark">
                      <FileUpload
                        v-if="!isMain && !isView"
                        :fileType="fileType"
                        :fileData="{ moduleName: moduleName }"
                        :fileList="formValue.problemAttachmentLists"
                        style="margin-bottom: 18px"
                        @changeFileList="problemAttachmentLists"
                        @saveFileStatus="saveFileStatus"
                      />
                    </px-form-item>
                  </px-col>
                </px-row>
              </template>
            </EditDropDown>
          </div>
          <div ref="improveTypeRef">
            <EditDropDown dropdownTitle="改善方案">
              <template v-slot:content>
                <px-row>
                  <px-col :span="12">
                    <px-form-item label="是否影响其他部门">
                      <px-radio-group v-model="formValue.influenceOtherFlag">
                        <px-radio v-for="(item, index) in influenceOtherFlagList" :key="index" :label="item.label">{{ item.value }}</px-radio>
                      </px-radio-group>
                    </px-form-item>
                  </px-col>
                  <px-col :span="12">
                    <px-form-item label="风险作业等级">
                      <px-select v-model="formValue.riskLevel" placeholder="请选择风险作业等级(非必填)">
                        <px-option v-for="item in riskLevelList" :key="item.value" :label="item.label" :value="item.value" />
                      </px-select>
                    </px-form-item>
                  </px-col>
                  <px-col :span="24">
                    <px-form-item label="方案简述" prop="planDesc">
                      <px-input
                        v-model="formValue.planDesc"
                        placeholder="请输入方案简述"
                        maxlength="1000"
                        show-word-limit
                        type="textarea"
                        clearable
                      />
                    </px-form-item>
                  </px-col>
                  <px-col :span="24">
                    <px-form-item label="方案详情" prop="planDetail">
                      <ReEditor v-model="formValue.planDetail" editorHeight="400px" placeholder="请输入方案详情" />
                    </px-form-item>
                  </px-col>
                  <px-col :span="24">
                    <px-form-item label="实施影响" prop="implementEffect">
                      <px-input
                        v-model="formValue.implementEffect"
                        placeholder="请输入实施影响"
                        maxlength="1000"
                        show-word-limit
                        type="textarea"
                        clearable
                      />
                    </px-form-item>
                  </px-col>
                  <px-col :span="24">
                    <px-form-item label="方案实施计划" prop="implementTime">
                      <px-input
                        v-model="formValue.implementTime"
                        placeholder="请输入方案实施计划"
                        type="text"
                        maxlength="50"
                        show-word-limit
                        clearable
                      />
                    </px-form-item>
                  </px-col>
                  <px-col :span="24">
                    <px-form-item label="效果预测" prop="effectPrediction">
                      <px-input
                        v-model="formValue.effectPrediction"
                        placeholder="请输入效果预测"
                        maxlength="1000"
                        show-word-limit
                        type="textarea"
                        clearable
                      />
                    </px-form-item>
                  </px-col>
                  <px-col :span="24">
                    <px-form-item label="附件上传" prop="remark">
                      <FileUpload
                        v-if="!isMain && !isView"
                        :fileType="fileType"
                        :fileData="{ moduleName: moduleName }"
                        :fileList="formValue.planAttachmentLists"
                        style="margin-bottom: 18px"
                        @changeFileList="planAttachmentLists"
                        @saveFileStatus="saveFileStatus"
                      />
                    </px-form-item>
                  </px-col>
                </px-row>
              </template>
            </EditDropDown>
          </div>
          <div ref="improveCostRef">
            <EditDropDown dropdownTitle="改善成本">
              <template v-slot:content>
                <px-row style="overflow: hidden">
                  <px-col :span="12">
                    <px-form-item label="是否增加费用" prop="increasedCostFlag">
                      <px-radio-group v-model="formValue.increasedCostFlag">
                        <px-radio v-for="(item, index) in increasedCostFlagList" :key="index" :label="item.name">{{ item.label }}</px-radio>
                      </px-radio-group>
                    </px-form-item>
                  </px-col>
                  <px-col v-if="formValue.increasedCostFlag === 'Y'" :span="12">
                    <px-form-item label="预估金额" prop="estimatedAmount">
                      <px-input v-model="formValue.estimatedAmount" placeholder="请输入预估金额" type="text" clearable maxlength="100" />
                    </px-form-item>
                  </px-col>
                  <px-col :span="24">
                    <px-form-item label="改善成本" prop="improveCost">
                      <ReEditor v-model="formValue.improveCost" editorHeight="400px" placeholder="请输入改善成本" />
                    </px-form-item>
                  </px-col>
                  <px-col :span="24">
                    <px-form-item label="附件上传" prop="remark">
                      <FileUpload
                        v-if="!isMain && !isView"
                        :fileType="fileType"
                        :fileData="{ moduleName: moduleName }"
                        :fileList="formValue.costAttachmentLists"
                        style="margin-bottom: 18px"
                        @changeFileList="costAttachmentLists"
                        @saveFileStatus="saveFileStatus"
                      />
                    </px-form-item>
                  </px-col>
                </px-row>
              </template>
            </EditDropDown>
          </div>
          <div ref="improveEvaluateRef">
            <EditDropDown dropdownTitle="改善评价">
              <template v-slot:content>
                <px-row>
                  <px-col :span="24">
                    <px-form-item label="效果评价" prop="effectEvaluate">
                      <px-input
                        v-model="formValue.effectEvaluate"
                        placeholder="请输入效果评价"
                        type="textarea"
                        show-word-limit
                        maxlength="1000"
                        clearable
                      />
                    </px-form-item>
                  </px-col>
                  <px-col :span="24">
                    <px-form-item label="附件上传" prop="remark">
                      <FileUpload
                        v-if="!isMain && !isView"
                        :fileType="fileType"
                        :fileData="{ moduleName: moduleName }"
                        :fileList="formValue.evaluateAttachmentLists"
                        style="margin-bottom: 18px"
                        @changeFileList="evaluateAttachmentLists"
                        @saveFileStatus="saveFileStatus"
                      />
                    </px-form-item>
                  </px-col>
                </px-row>
              </template>
            </EditDropDown>
          </div>
        </px-form>
      </px-scrollbar>
      <WidgetRouter ref="widgetRef" :router-list="routerList" :scrollbar-ref="scrollbarRef" :scroll-top="scrollTop" />
      <div class="info-form-btns">
        <px-button @click="backMainPage">取消</px-button>
        <px-button type="primary" :disabled="fileStatusFlag" @click="handleSubmit(formRef)">保存</px-button>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { getDefect } from "@/api/defect";
import { createImprove, getImproveId, updateImprove, viewImprove } from "@/api/equip/improve";
import ReEditor from "@/components/ReEditor/index.vue";
import FileUpload from "@/components/Upload/FileUpload.vue";
import EditDropDown from "@/views/all/equip/components/EditDropDown.vue";
import EquipClassSelect from "@/views/all/equip/components/EquipClassSelect.vue";
import EquipSystemTree from "@/views/all/equip/components/EquipSystemTree.vue";
import { WidgetRouter } from "@/views/erc/inspect/components/index";
import { debounce } from "@pureadmin/utils";
import { PxMessage, PxMessageBox } from "px-ui";
import { computed, onBeforeMount, reactive, ref, toRefs, watch, onMounted } from "vue";
import { getEmployeeIdAndName } from "@/utils/pengxi";

// 调用父级-返回主页、查询列表方法
const emits = defineEmits(["backMainPage", "queryTable"]);
// 父级传过来的值
const props = defineProps([
  "isMain",
  "isView",
  "isDraftVisible",
  "missingId",
  "missingLevelList",
  "missingTypeList",
  "missingResourceList",
  // "missingOwnerList",
  "rectifyStatusList",
  "reportUserList",
  "drawerType",
  "relationNumber"
]);
// 显示隐藏
const isMain = computed(() => props.isMain);
const isView = computed(() => props.isView);
//右侧滚动导航
const scrollbarRef = ref(null);
const routerList = ref([]);
const scrollTop = ref();
//折叠面板ref
const widgetRef = ref();
const basicsRef = ref(null);
const improveViewRef = ref(null);
const improveTypeRef = ref(null);
const improveCostRef = ref(null);
const improveEvaluateRef = ref(null);

// 页面本身的值
const formRef = ref();
const state = reactive<any>({
  formValue: {
    improveId: "",
    improveName: "",
    systemId: "",
    systemName: "",
    className: "",
    improveResource: "CUSTOM",
    relationNumber: "",
    reasonType: "",
    necessaryLevel: "HIGH",
    applicableScope: "",
    newFactoryFlag: "Y",
    improveStatus: "UNDO",
    improveUser: "",
    problemDesc: "",
    rootCause: "",
    influenceScope: "",
    problemAttachment: "",
    planDesc: "",
    planDetail: "",
    implementEffect: "",
    implementTime: "",
    effectPrediction: "",
    planAttachment: "",
    increasedCostFlag: "Y",
    estimatedAmount: "",
    improveCost: "",
    costAttachment: "",
    effectEvaluate: "",
    evaluateAttachment: "",
    influenceOtherFlag: "否",
    problemAttachmentLists: [], //问题附件
    planAttachmentLists: [], //方案附件
    costAttachmentLists: [], //成本附件
    evaluateAttachmentLists: [] //评价附件
  },
  increasedCostFlagList: [
    { name: "Y", label: "是" },
    { name: "N", label: "否" }
  ],
  fileStatusFlag: false,
  fileType: "add",
  moduleName: "",
  missingOwnerList: [], //改善人
  riskLevelList: [
    {
      value: "无",
      label: "无"
    },
    {
      value: "一级风险（稍有风险）",
      label: "一级风险（稍有风险）"
    },
    {
      value: "二级风险（一般风险）",
      label: "二级风险（一般风险）"
    },
    {
      value: "三级风险（显著风险）",
      label: "三级风险（显著风险）"
    },
    {
      value: "四级风险（高度风险）",
      label: "四级风险（高度风险）"
    },
    {
      value: "五级风险（极高风险）",
      label: "五级风险（极高风险）"
    }
  ],
  influenceOtherFlagList: [
    {
      label: "否",
      value: "否"
    },
    {
      label: "是",
      value: "是"
    }
  ]
});
const { formValue, increasedCostFlagList, fileStatusFlag, fileType, moduleName, missingOwnerList, riskLevelList, influenceOtherFlagList } =
  toRefs(state);
// 表单校验规则
const rules = computed(() => ({
  improveName: [{ required: true, message: "改善名称不能为空", trigger: "blur" }],
  reasonType: [{ required: true, message: "原因类型不能为空", trigger: "change" }],
  necessaryLevel: [{ required: true, message: "改善必要性不能为空", trigger: "change" }],
  applicableScope: [{ required: true, message: "适用范围不能为空", trigger: "blur" }],
  newFactoryFlag: [{ required: true, message: "新建厂follow不能为空", trigger: "change" }],
  improveStatus: [{ required: true, message: "改善状态不能为空", trigger: "change" }],
  problemDesc: [{ required: true, message: "问题描述不能为空", trigger: "blur" }],
  rootCause: [{ required: true, message: "根本原因不能为空", trigger: "blur" }],
  influenceScope: [{ required: true, message: "影响范围不能为空", trigger: "blur" }],
  planDesc: [{ required: true, message: "方案简述不能为空", trigger: "blur" }],
  implementEffect: [{ required: true, message: "实施影响不能为空", trigger: "blur" }],
  implementTime: [{ required: true, message: "方案实施计划不能为空", trigger: "change" }],
  effectPrediction: [{ required: true, message: "效果预测不能为空", trigger: "blur" }],
  increasedCostFlag: [{ required: true, message: "增加费用不能为空", trigger: "blur" }],
  estimatedAmount: [{ required: true, message: "预估金额不能为空", trigger: "blur" }],
  effectEvaluate: [{ required: true, message: "效果评价不能为空", trigger: "blur" }],
  planDetail: [{ required: true, validator: checkPlanDetail, trigger: "blur" }], //方案详情
  improveCost: [{ required: true, validator: checkImproveCost, trigger: "blur" }] //改善成本
}));
// 获取当前改善人接口
const focusRecorder = async (val?) => {
  const param = formValue.value.className ? [formValue.value.className] : [];
  const data = await getEmployeeIdAndName(param);
  missingOwnerList.value = data.map(item => {
    return {
      value: item.label,
      label: item.label
    };
  });
};
// 监视isMain和isView值
watch(isMain, async val => {
  if (!val && !props.isView) {
    formRef.value?.resetFields();
    focusRecorder();
    if (props.drawerType === "add") {
      const data = await getImproveId({});
      formValue.value.improveId = data;
      if (props.relationNumber) {
        //有值，说明是从缺失管理跳转过来的
        formValue.value.relationNumber = props.relationNumber;
        formValue.value.improveResource = "MISSING";
        getDefectDetail(); //缺失管理页面的详情接口
      } else {
        formValue.value.relationNumber = "";
        formValue.value.improveResource = "CUSTOM";
        formValue.value.improveName = "";
        formValue.value.systemId = "";
        formValue.value.systemName = "";
        formValue.value.className = "";
        formValue.value.reasonType = "";
        formValue.value.necessaryLevel = "HIGH";
        formValue.value.applicableScope = "";
        formValue.value.newFactoryFlag = "Y";
        formValue.value.improveStatus = "UNDO";
        formValue.value.improveUser = "";
        formValue.value.problemDesc = "";
        formValue.value.rootCause = "";
        formValue.value.influenceScope = "";
        formValue.value.problemAttachment = "";
        formValue.value.planDesc = "";
        formValue.value.planDetail = "";
        formValue.value.implementEffect = "";
        formValue.value.implementTime = "";
        formValue.value.effectPrediction = "";
        formValue.value.planAttachment = "";
        formValue.value.increasedCostFlag = "Y";
        formValue.value.estimatedAmount = "";
        formValue.value.improveCost = "";
        formValue.value.costAttachment = "";
        formValue.value.effectEvaluate = "";
        formValue.value.evaluateAttachment = "";
        formValue.value.improveUser = "";
        formValue.value.riskLevel = "";
        formValue.value.influenceOtherFlag = "否";
        delete formValue.value.id;
        fileType.value = "add";
      }
    } else if (props.drawerType === "edit") {
      getDetail();
      fileType.value = "edit";
    }
  } else {
    formValue.value = {
      improveId: "",
      improveName: "",
      systemId: "",
      systemName: "",
      className: "",
      improveResource: "CUSTOM",
      relationNumber: "",
      reasonType: "",
      necessaryLevel: "HIGH",
      applicableScope: "",
      newFactoryFlag: "Y",
      improveStatus: "UNDO",
      improveUser: "",
      problemDesc: "",
      rootCause: "",
      influenceScope: "",
      problemAttachment: "",
      planDesc: "",
      planDetail: "",
      implementEffect: "",
      implementTime: "",
      effectPrediction: "",
      planAttachment: "",
      increasedCostFlag: "Y",
      estimatedAmount: "",
      improveCost: "",
      costAttachment: "",
      effectEvaluate: "",
      evaluateAttachment: "",
      riskLevel: "",
      influenceOtherFlag: "否",
      problemAttachmentLists: [], //问题附件
      planAttachmentLists: [], //方案附件
      costAttachmentLists: [], //成本附件
      evaluateAttachmentLists: [] //评价附件
    };
  }
  // focusMissingPosition();
});
onBeforeMount(() => {
  scrollInit();
});
// 校验方案详情
const checkPlanDetail = (rule: any, value: any, callback: any) => {
  if (!formValue.value.planDetail || formValue.value.planDetail === "<p><br></p>") {
    callback(new Error("方案详情不能为空"));
  } else {
    callback();
  }
};
// 校验改善成本
const checkImproveCost = (rule: any, value: any, callback: any) => {
  if (!formValue.value.improveCost || formValue.value.improveCost === "<p><br></p>") {
    callback(new Error("改善成本不能为空"));
  } else {
    callback();
  }
};
//右侧滚动导航
const scrollInit = () => {
  let routerName = "基本信息,问题详情,改善方案,改善成本,改善评价";
  let routerRefs = [basicsRef, improveViewRef, improveTypeRef, improveCostRef, improveEvaluateRef];
  routerList.value = routerName.split(",").map((item, index) => ({ ref: routerRefs[index], name: item }));
};
//右侧滚动导航相关函数
const handleScroll = debounce(({ scrollTop: curScrollTop }) => {
  scrollTop.value = curScrollTop;
}, 200);
// 获取详情接口
const getDetail = async () => {
  const params = {
    improveId: props.missingId
  };
  const data: any = await viewImprove(params);
  formValue.value.improveId = data.improveId;
  formValue.value.improveName = data.improveName;
  formValue.value.systemId = data.systemId;
  formValue.value.systemName = data.systemName;
  formValue.value.className = data.className;
  formValue.value.improveResource = data.improveResource;
  formValue.value.relationNumber = data.relationNumber;
  formValue.value.reasonType = data.reasonType;
  formValue.value.necessaryLevel = data.necessaryLevel;
  formValue.value.applicableScope = data.applicableScope;
  formValue.value.newFactoryFlag = data.newFactoryFlag;
  formValue.value.improveStatus = data.improveStatus;
  formValue.value.improveUser = data.improveUser;
  formValue.value.problemDesc = data.problemDesc;
  formValue.value.rootCause = data.rootCause;
  formValue.value.influenceScope = data.influenceScope;
  formValue.value.problemAttachment = data.problemAttachment;
  formValue.value.planDesc = data.planDesc;
  formValue.value.planDetail = data.planDetail;
  formValue.value.implementEffect = data.implementEffect;
  formValue.value.implementTime = data.implementTime;
  formValue.value.effectPrediction = data.effectPrediction;
  formValue.value.planAttachment = data.planAttachment;
  formValue.value.increasedCostFlag = data.increasedCostFlag;
  formValue.value.estimatedAmount = data.estimatedAmount;
  formValue.value.improveCost = data.improveCost;
  formValue.value.costAttachment = data.costAttachment;
  formValue.value.effectEvaluate = data.effectEvaluate;
  formValue.value.evaluateAttachment = data.evaluateAttachment;
  formValue.value.influenceOtherFlag = data.influenceOtherFlag;
  formValue.value.improveUser = data.improveUser;
  formValue.value.riskLevel = data.riskLevel;

  if (data.problemAttachmentList && data.problemAttachmentList.length > 0) {
    data.problemAttachmentList.forEach(item => {
      item.url = item.fileUrl;
      item.name = item.originalName;
    });
    formValue.value.problemAttachmentLists = data.problemAttachmentList;
  } else {
    formValue.value.problemAttachmentLists = [];
  }
  if (data.planAttachmentList && data.planAttachmentList.length > 0) {
    data.planAttachmentList.forEach(item => {
      item.url = item.fileUrl;
      item.name = item.originalName;
    });
    formValue.value.planAttachmentLists = data.planAttachmentList;
  } else {
    formValue.value.planAttachmentLists = [];
  }
  if (data.costAttachmentList && data.costAttachmentList.length > 0) {
    data.costAttachmentList.forEach(item => {
      item.url = item.fileUrl;
      item.name = item.originalName;
    });
    formValue.value.costAttachmentLists = data.costAttachmentList;
  } else {
    formValue.value.costAttachmentLists = [];
  }
  if (data.evaluateAttachmentList && data.evaluateAttachmentList.length > 0) {
    data.evaluateAttachmentList.forEach(item => {
      item.url = item.fileUrl;
      item.name = item.originalName;
    });
    formValue.value.evaluateAttachmentLists = data.evaluateAttachmentList;
  } else {
    formValue.value.evaluateAttachmentLists = [];
  }
};
// 获取缺失管理详情
const getDefectDetail = async () => {
  const params = {
    missingId: props.relationNumber
  };
  const data: any = await getDefect(params);
  formValue.value.systemId = data.systemId;
  formValue.value.className = data.className;
};
// 返回主页面
const backMainPage = () => {
  PxMessageBox.confirm("确认退出页面？退出后内容不会保存", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
    draggable: true
  })
    .then(async () => {
      formRef.value?.resetFields();
      emits("backMainPage");
      emits("queryTable");
    })
    .catch(() => {});
};
// 提交
const handleSubmit = async (formEl: any) => {
  const userInfo = JSON.parse(localStorage.getItem("userInfo"));
  if (!formEl) return;
  await formEl.validate(async valid => {
    if (valid) {
      PxMessageBox.confirm("确认要保存改善管理吗？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
        draggable: true
      }).then(async () => {
        const params = formValue.value;
        const arr = [];
        if (formValue.value.problemAttachmentLists && formValue.value.problemAttachmentLists.length > 0) {
          formValue.value.problemAttachmentLists.forEach(item => {
            if (item?.id) {
              arr.push(item.id);
            } else if (item?.response.data.id) {
              arr.push(item.response.data.id);
            }
          });
        }
        params.problemAttachment = arr.toString();
        const arr1 = [];
        if (formValue.value.planAttachmentLists && formValue.value.planAttachmentLists.length > 0) {
          formValue.value.planAttachmentLists.forEach(item => {
            if (item?.id) {
              arr1.push(item.id);
            } else if (item?.response.data.id) {
              arr1.push(item.response.data.id);
            }
          });
        }
        params.planAttachment = arr1.toString();
        const arr2 = [];
        if (formValue.value.costAttachmentLists && formValue.value.costAttachmentLists.length > 0) {
          formValue.value.costAttachmentLists.forEach(item => {
            if (item?.id) {
              arr2.push(item.id);
            } else if (item?.response.data.id) {
              arr2.push(item.response.data.id);
            }
          });
        }
        params.costAttachment = arr2.toString();
        const arr3 = [];
        if (formValue.value.evaluateAttachmentLists && formValue.value.evaluateAttachmentLists.length > 0) {
          formValue.value.evaluateAttachmentLists.forEach(item => {
            if (item?.id) {
              arr3.push(item.id);
            } else if (item?.response.data.id) {
              arr3.push(item.response.data.id);
            }
          });
        }
        params.evaluateAttachment = arr3.toString();
        if (props.drawerType === "add") {
          await createImprove(params);
          PxMessage.success("保存改善管理成功");
          emits("backMainPage");
          emits("queryTable");
        } else if (props.drawerType === "edit") {
          await updateImprove(params);
          PxMessage.success("修改改善管理成功");
          emits("backMainPage");
          emits("queryTable");
        }
      });
    }
  });
};
//问题附件上传
const problemAttachmentLists = val => {
  formValue.value.problemAttachmentLists = val;
};
//计划附件上传
const planAttachmentLists = val => {
  formValue.value.planAttachmentLists = val;
};
//改善附件上传
const costAttachmentLists = val => {
  formValue.value.costAttachmentLists = val;
};
//评价附件上传
const evaluateAttachmentLists = val => {
  formValue.value.evaluateAttachmentLists = val;
};
// 文件是否上传完，是否可以保存
const saveFileStatus = val => {
  fileStatusFlag.value = val;
};

// 外部封装好的组件
// 科室组件调用父组件
const changeClassName = (val, isOnlyClass) => {
  formValue.value.className = val;
  if (!isOnlyClass) {
    formRef.value?.resetFields(["systemId"]);
  }
  focusRecorder(val);
};
// 系统组件调用父组件
const changeAllSystemCodes = (val, val2, val3, val4) => {
  formValue.value.className = val2;
  formValue.value.systemId = val3;
};
// 系统组件调用父组件
const changeSystemCodes = (val, val2) => {
  formValue.value.systemId = val;
};
// 系统组件值改变触发
const clearSystem = val => {
  formRef.value?.resetFields(["systemId"]);
};
</script>
<style lang="scss" scoped>
@import url("@/views/all/equip/scss/add-detail.scss");

.add-inspect-plan {
  // display: flex;
  width: 100%;
  height: calc(100vh - 195px);
  padding: 0 24px 24px;
  overflow-x: auto;
  background-color: #fff;
  border-radius: 8px;

  ._form-rules {
    :deep(.px-form-item__content) {
      flex-direction: column;
      align-items: flex-start;
    }
  }

  .add-inspect-plan-content {
    :deep(.px-form) {
      width: 100%;

      .px-form-item {
        width: 100%;
        margin-right: 0;

        .px-input {
          width: 100%;
        }

        .px-select {
          width: 100%;
        }

        .px-select-v2 {
          width: 100%;
        }

        .px-input-number {
          width: 100%;

          .px-input__inner {
            text-align: left;
          }
        }

        &.is-error {
          .px-input-number {
            .px-input__wrapper {
              border: 1px solid var(--px-color-danger);
              box-shadow: none;
            }
          }
        }
      }
    }

    .equip-table {
      --px-table-border-color: #dfdfdf;

      margin-bottom: 18px;
      font-size: 14px;

      :deep(.px-table__header) {
        thead {
          tr {
            background-color: #e7eefb !important;

            th {
              font-size: 14px;
              font-weight: bold;
              color: #000;
              background-color: #e7eefb !important;
            }
          }
        }
      }

      :deep(.px-input-number) {
        width: 100%;
      }
    }
  }

  &-wrapper {
    flex: 1 1 auto;
    min-width: 1350px;
  }

  .info-form-btns {
    position: absolute;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    width: 100%;
    height: 68px;
    padding-right: 24px;
    background: #fff;
    box-shadow: 0 0 10px 0 rgb(0 0 0 / 16%);
  }
}
.add-equip-detail .add-equip-content {
  height: calc(100vh - 284px);
}
.add-inspect-plan-wrapper {
  display: flex;
}
:deep(.px-form .px-form-item__label) {
  width: 126px !important;
}
</style>
